﻿using Liju.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace LiJu.Db
{
    /// <summary>
    /// 螺丝仓设置及库存
    /// </summary>
    public class ScrewDbHelper
    {
        /// <summary>
        /// 获取螺丝仓信息列表
        /// </summary>
        /// <returns></returns>
        public static List<ScrewModel> GetScrewModelList()
        {
            string strMySql = $"select * from screwmodel ";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            return DbExtension.DataTableToList<ScrewModel>(dt);
        }

        /// <summary>
        /// 查询螺钉信息
        /// </summary>
        /// <returns></returns>
        public static ScrewModel GetScrewModelByID(int nID)
        {
            //获取没有删除的产品
            string strMySql = $"select * from screwmodel where Id={nID} ";
            DataTable dt = DalMySQL.ExecuteDataTable(strMySql);
            List<ScrewModel> listScrewModel = DbExtension.DataTableToList<ScrewModel>(dt);
            if (listScrewModel != null && listScrewModel.Count > 0)
            {
                return listScrewModel[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 查询螺钉物料编码 add ming
        /// </summary>
        /// <returns></returns>
        public static string GetScrewModelByMac(string mac)
        {
            //获取没有删除的产品
            string strSql = $"select WuLiaoCode from screwmodel where MAC='{mac}' ";
            return DalMySQL.GetSingle(strSql).ToString();
        }

        /// <summary>
        /// 螺钉机送钉成功 剩余库存数量-1 
        /// </summary>
        public static int ScrewModelUsed(int ScrewModlID)
        {
            string strMySql =  $"update screwmodel set CapacityNow=CapacityNow-1 where id={ScrewModlID};";
            return DalMySQL.ExecuteNonQuery(strMySql);
        }
        
        /// <summary>
        /// NG螺钉应丢进废螺丝仓数量+1
        /// </summary>
        public static int ScrewModelNGNumAdd(int ScrewModlID)
        {
            string strMySql = $"update screwmodel set CapacityNGNum=CapacityNGNum+1 where id={ScrewModlID};" +
                "update screwmodel set CapacityNGNum=CapacityNGNum+1 where id=3;";
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// NG螺钉实际丢进废弃螺钉仓数量+1
        /// </summary>
        public static int ScrewModelNGNumRealAdd()
        {
            string strMySql =  $"update screwmodel set CapacityNow=CapacityNow+1 where id=3;";
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 废螺丝仓开锁 螺丝仓12 废螺丝仓 ng数量重置为0
        /// </summary>
        public static int ResetLuoSiCangNgNum()
        {
            string strMySql = $"update screwmodel set CapacityNGNum=0; update screwmodel set CapacityNow=0 where id=3;";
            return DalMySQL.ExecuteNonQuery(strMySql);
        }

        /// <summary>
        /// 查询螺钉物料编码
        /// </summary>
        /// <returns></returns>
        public static string GetMaterialCodeById(int lockId)
        {
            //获取没有删除的产品
            string strSql = $"select WuLiaoCode from screwmodel where Id={lockId} ";
            return DalMySQL.GetSingle(strSql).ToString();
        }

    }
}
